home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / CPP / ACGIFREE.ZIP / INCLUDE / A_ARRAY.H < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-08  |  8.8 KB  |  231 lines

  1. //////////////////////////////////////////////////////////////////////
  2. // AArray base class based on ABaseElement, essentially a BYTE array
  3. //////////////////////////////////////////////////////////////////////
  4. class AArray : public ABaseElement
  5. {
  6.   public:
  7.     AArray(AArray *paSource = NULL);    //a_Default constructor/copy constructor!
  8.     ~AArray();
  9.     
  10.     //a_Declares debug/dump related functions
  11.     #ifdef _DEBUG_DUMP_
  12.       void dump(void);     //a_Debugging dump, when _DEBUG_DUMP_ is set
  13.     #endif
  14.  
  15.     //a_Operatot access
  16.     BYTE &operator [] (int iBYTEPos) { return _aGetBYTERef(iBYTEPos); }
  17.     AArray &operator = (const AArray &aSource) { _bCopy(aSource); return *this; }
  18.  
  19.     //a_Declaration of access/assignment to elements of array
  20.     virtual int  aGetAt(int iPos) const { return _aGetBYTEValue(iPos); }
  21.     virtual void aSetAt(int iPos, int iState, int iMode = BLIT_COPY) { aSetBYTEAt(iPos, iState, iMode); }
  22.     virtual int  aIsSet(int iPos) const { return _aGetBYTEValue(iPos); }
  23.  
  24.     //a_Range setting function, defaults toclearing all to 0x0, uses the virtual access members
  25.     void aSetRange(int iState = 0x0, int iMode = BLIT_COPY, int iStart = 0x0, int iEnd = -0x1);
  26.  
  27.     //a_Array BYTE access/assignment
  28.     void aSetBYTEAt(int iBYTEPos, BYTE bMask, int iMode = BLIT_COPY);
  29.     BYTE aGetBYTEAt(int iBYTEPos) { return _aGetBYTEValue(iBYTEPos); }
  30.  
  31.     //a_Setting array size, assigning it to an external buffer, ...
  32.     virtual void aSetFrom(const void *pcvSource, int iLength) { _aSetToBYTEArray((const BYTE *)pcvSource, iLength); }
  33.     virtual int  aSetSize(int iNewSize, int iRealloc = 0x0); 
  34.      
  35.     //a_Access to the size is child dependent, this is a BYTE array so the mapping is 1:1; ABitArray is 8 times the allocated size (or thereabouts)
  36.     virtual int aGetSize(void) const  { return m_iSize; } 
  37.     
  38.     //a_True size access
  39.     const BYTE *aGetArray(void) const     { return m_pData; }
  40.     int         aGetAllocSize(void) const { return m_iAllocSize; }
  41.  
  42.     //a_Blitting function; length of -1 is to blit to the end of the smaller one
  43.     virtual void aBlit(int iMyStart, AArray &aSource, int iSourceStart, int iLength = -1, int iMode = BLIT_COPY);
  44.  
  45.     //ABaseElement overrides
  46.     virtual void doOut(AStreamOutput *pasOut) const { _doArrayOutput(pasOut); }
  47.  
  48.     //a_AXBitmap output (image/x-xbitmap format array), mirroring the BYTEs
  49.     void aDoXBitmap(AStreamOutput *pasOut) const { _doArrayOutput(pasOut, 0x1); }
  50.  
  51.   protected:
  52.     //a_Array control methods
  53.     void _bCopy(const AArray &aSource);
  54.     void _aSetToBYTEArray(const BYTE *pcbSource, int iLength);
  55.     virtual int _aMapAllocToSize(int iAllocated) { return iAllocated; }  //a_This is a BYTE array
  56.     
  57.     //a_BYTE control
  58.     BYTE _aGetBYTEValue(int iBYTEPos) const
  59.     {
  60.       assert(iBYTEPos >= 0x0 && iBYTEPos < m_iAllocSize);
  61.       if (m_pData)
  62.         return m_pData[iBYTEPos];   //a_Only a copy of the byte!
  63.       else
  64.         return 0x0;
  65.     }
  66.     BYTE &_aGetBYTERef(int iBYTEPos)
  67.     {
  68.       assert(iBYTEPos >= 0x0 && iBYTEPos < m_iAllocSize);
  69.       assert(m_pData);
  70.       if (m_pData) return m_pData[iBYTEPos];   //a_The BYTE itself
  71.       else         return m_bSafetyBYTE;       //a_If the array doesn't exist...
  72.     }
  73.     
  74.     //a_Ouput array elements in HEX form separated by comma and 8 per line
  75.     //a_ for PBM image iPBM=1 then each bit is mirrored (reversed) as per spec
  76.     void _doArrayOutput(AStreamOutput *pasOut, int iPBM = 0x0) const;
  77.     
  78.     //a_Bit counting
  79.     int _aBitCount(BYTE bX) const
  80.     {
  81.       //a_# of bits in a given offset
  82.       static char sm_bC[0x10] = { 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4 };
  83.  
  84.       //a_# of bits in a BYTE
  85.       return sm_bC[bX & '\xF'] + sm_bC [(bX >> 0x4) & '\xF'];
  86.     }
  87.  
  88.     //a_The array and it's properties
  89.     BYTE *m_pData;              //a_Actual data
  90.     int   m_iAllocSize;         //a_Allocated size in BYTEs (true size)
  91.     int   m_iSize;              //a_Specified length of array (depends on implementation!) (ie. in bit array this is 8 times the allocated size)
  92.     BYTE  m_bSafetyBYTE;        //a_If the array is NULL, to prevent problems, a ref to this is returned
  93. };
  94.  
  95. //////////////////////////////////////////////////////////////////////
  96. // ABit - holds one bit and modifies it, used by ABitArray
  97. //////////////////////////////////////////////////////////////////////
  98. class ABit
  99. {
  100.   public:
  101.     ABit() { m_pbData = NULL; m_iBitPos = 0x0; }
  102.  
  103.     friend class ABitArray;             //a_For setting the protected memebers of this class
  104.  
  105.     void operator =(const ABit &bSource)
  106.     {
  107.       m_pbData  = bSource.m_pbData;      //a_Same pointer!
  108.       m_iBitPos = bSource.m_iBitPos;
  109.     }
  110.  
  111.     void operator =(int iState)
  112.     {
  113.       //a_Assignment
  114.       assert(m_iBitPos < 0x8 && m_iBitPos >= 0x0 && m_pbData);
  115.       if (iState) *m_pbData |= (0x1 << m_iBitPos);
  116.       else        *m_pbData &= ~(0x1 << m_iBitPos);
  117.     }
  118.  
  119.     ABit &operator &=(int iState)
  120.     {
  121.       assert(m_iBitPos < 0x8 && m_iBitPos >= 0x0 && m_pbData);
  122.       if (iState) *m_pbData &= (0x1 << m_iBitPos);
  123.       else        *m_pbData &= ~(0x1 << m_iBitPos);
  124.       return *this;
  125.     }
  126.      
  127.     ABit &operator |=(int iState)
  128.     {
  129.       assert(m_iBitPos < 0x8 && m_iBitPos >= 0x0 && m_pbData);
  130.       if (iState) *m_pbData |= (0x1 << m_iBitPos);       //a_OR only matters with 1!
  131.       return *this;
  132.     }
  133.  
  134.     ABit &operator ^=(int iState)
  135.     {
  136.       assert(m_iBitPos < 0x8 && m_iBitPos >= 0x0 && m_pbData);
  137.       if (iState) *m_pbData ^= (0x1 << m_iBitPos);      //a_XOR only matters with 1!
  138.       return *this;
  139.     }
  140.  
  141.     operator int() const
  142.     { 
  143.       //a_Cast to an integer
  144.       assert(m_iBitPos < 0x8 && m_iBitPos >= 0x0 && m_pbData);
  145.       return ((*m_pbData & (0x1 << m_iBitPos)) ? 0x1 : 0x0);
  146.     }
  147.  
  148.   protected:
  149.     BYTE *m_pbData;               //a_Pointer to the 8bit in question
  150.     int   m_iBitPos;              //a_Which bit in this BYTE
  151. };
  152.  
  153. //////////////////////////////////////////////////////////////////////
  154. // ABitArray - Array of 8 bits packed into a BYTE :)
  155. //////////////////////////////////////////////////////////////////////
  156. class ABitArray : public AArray
  157. {
  158.   public:
  159.     ABitArray(ABitArray *pbaSource = NULL);    //a_Default and Copy constructor!
  160.     ~ABitArray();
  161.     
  162.     //a_Equals operator (for tag copying)
  163.     ABitArray &operator =(const ABitArray &baSource)   
  164.       { _bCopy(baSource); return *this; }
  165.  
  166.     //a_Declares debug/dump related functions
  167.     #ifdef _DEBUG_DUMP_
  168.       void dump(void);     //a_Debugging dump, when _DEBUG_DUMP_ is set
  169.     #endif
  170.  
  171.     //a_Virtual overrides from AArray
  172.     int  aSetSize(int iNewBitSize, int iRealloc = 0x0);   //a_Change the size, iRealloc is valid only when shrinking and needing to reallocate
  173.     int  aGetAt(int iBitPos) const { return (_baGetAt(iBitPos) ? 0x1: 0x0); }
  174.     int  aIsSet(int iBitPos) { return _baGetAt(iBitPos); }
  175.     void aSetAt(int iBitPos, int iState, int iMode = 0x0);
  176.  
  177.     //a_Set
  178.     ABit &operator[] (int iBitPos);                     //a_Also a get
  179.  
  180.   protected:
  181.     //a_Mapping from actual allocated size to a bit size
  182.     virtual int _aMapAllocToSize(int iAllocated) { return iAllocated * 0x8; }  //a_8 elements in each allocated unit
  183.  
  184.     //a_Access functions
  185.     int _baGetAt(int iBitPos) const
  186.     {
  187.       //a_Gets the state of the bit (0 or 1)
  188.       assert(iBitPos >= 0x0 && iBitPos < aGetSize());
  189.       return int((m_pData[iBitPos / 0x8]) & BYTE(0x1UL << (0x7 - (iBitPos % 0x8))));
  190.     }
  191.  
  192.     BYTE *_baGetBYTEFromBit(int iBitPos) const
  193.     {
  194.       //a_Gets the BYTE that the bit is in
  195.       assert(iBitPos >= 0x0 && iBitPos < aGetSize());
  196.       return &m_pData[iBitPos / 0x8];
  197.     }
  198.     
  199.     int _baIsSetInBYTE(BYTE bX, int iBitPos) const
  200.     {
  201.       //a_Is set bit N in 8 bit BYTE
  202.       assert(iBitPos < 8 && iBitPos >= 0);
  203.       return (bX & (0x1 << iBitPos));
  204.     }
  205.  
  206.     int _baIsClearInBYTE(BYTE &bX, int iBitPos) const
  207.     {
  208.       //a_Is clear bit N in a 8 bit BYTE
  209.       assert(iBitPos < 8 && iBitPos >= 0);
  210.       return (bX & ~(0x1 << iBitPos));
  211.     }
  212.  
  213.     void _baSetBitInBYTE(BYTE &bX, int iBitPos, int iState)
  214.     {
  215.       //a_Sets the bit N in a 8 bit BYTE
  216.       assert(iBitPos < 8 && iBitPos >= 0);
  217.       if (iState) bX |= (0x1 << iBitPos);     //a_Set
  218.       else        bX &= ~(0x1 << iBitPos);    //a_Clear
  219.     }
  220.  
  221.     //a_The array and it's properties
  222.     ABit m_bIt;                  //a_Internal bit struct for modification purposes
  223.     void _baSetABit(int iBitPos)
  224.     {
  225.       //a_This function intentionally prevents reallocation.  Use SeAt instead.  To prevent runaway allocations.
  226.       assert(iBitPos < aGetSize() && iBitPos >= 0x0);
  227.       m_bIt.m_pbData  = _baGetBYTEFromBit(iBitPos);
  228.       m_bIt.m_iBitPos = 0x7 - (iBitPos % 0x8);
  229.     }
  230. };
  231.